package com.ruoyi.project.digital.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.ruoyi.project.cms.domain.WxSubscribeMsg;
import com.ruoyi.project.cms.domain.enums.MsgCode;
import com.ruoyi.project.cms.service.IWxSubscribeMsgService;
import com.ruoyi.project.common.utils.SubscribeMsgUtils;
import com.ruoyi.project.digital.domain.Apply;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: zxht
 * @Date: 2020/12/17 15:54
 * @Author: syc
 * @Description:小程序模板消息
 */
@Component
public class MiniAppTemplateMsgService {
    @Autowired
    private SubscribeMsgUtils subscribeMsgUtils;
    @Autowired
    private IWxSubscribeMsgService wxSubscribeMsgService;

    /**
     * 申请成功通知
     */
    public void applySuccessMsg(String openId, Apply apply){
        WxSubscribeMsg wsm = wxSubscribeMsgService.selectByCode(MsgCode.APPLY_SUCCESS.getValue());
        if (ObjectUtil.isNotEmpty(wsm)) {
            wsm.setPage(wsm.getPage()+"?applyId="+apply.getId());
            wsm.setValue1(apply.getName());
            wsm.setValue2(DateUtil.offsetMonth(apply.getCreateTime(),1).toString());
            wsm.setValue3("打印申请");
            subscribeMsgUtils.sendMsgTemplate(openId, wsm);
        }
    }

    /**
     * 审核结果通知
     */
    public void auditResultMsg(Long id,String openId,boolean flag){
        //小程序模板消息
        WxSubscribeMsg wsm = wxSubscribeMsgService.selectByCode(MsgCode.AUDIT_RESULT.getValue());
        if (ObjectUtil.isNotEmpty(wsm)) {
            wsm.setPage(wsm.getPage()+"?applyId="+id);
            wsm.setValue1("系统管理员");
            wsm.setValue3(DateUtil.now());
            wsm.setValue2(flag?"成功":"失败");
            subscribeMsgUtils.sendMsgTemplate(openId, wsm);
        }
    }

    /**
     * 待支付通知
     */
    public void waitPayMsg(Long id,String openId, String applyNo, BigDecimal amount){
        //小程序模板消息
        WxSubscribeMsg wsm = wxSubscribeMsgService.selectByCode(MsgCode.WAIT_PAY.getValue());
        if (ObjectUtil.isNotEmpty(wsm)) {
            wsm.setPage(wsm.getPage()+"?applyId="+id);
            wsm.setValue1(applyNo);
            wsm.setValue2(amount+"元");
            wsm.setValue3("为不影响您继续使用，请及时支付");
            subscribeMsgUtils.sendMsgTemplate(openId, wsm);
        }
    }

    /**
     * 支付通知
     */
    public void finidedPayMsg(Long id ,String openId, String applyNo, BigDecimal amount){
        //小程序模板消息
        WxSubscribeMsg wsm = wxSubscribeMsgService.selectByCode(MsgCode.FINISHED_PAY.getValue());
        if (ObjectUtil.isNotEmpty(wsm)) {
            wsm.setPage(wsm.getPage()+"?applyId="+id);
            wsm.setValue1(applyNo);
            wsm.setValue2(amount+"元");
            wsm.setValue3("支付成功");
            subscribeMsgUtils.sendMsgTemplate(openId, wsm);
        }
    }

    /**
     * 邮寄通知
     */
    public void mailedMsg(String openId, Apply apply){
        //小程序模板消息
        WxSubscribeMsg wsm = wxSubscribeMsgService.selectByCode(MsgCode.MAILED.getValue());
        if (ObjectUtil.isNotEmpty(wsm)) {
            wsm.setPage(wsm.getPage()+"?applyId="+apply.getId());
            wsm.setValue1("邮政快递");
            wsm.setValue2(apply.getExpressNo());
            wsm.setValue3(DateUtil.now());
            wsm.setValue4(apply.getProvince()+apply.getCity()+apply.getCounty()+apply.getAddressDetails());
            subscribeMsgUtils.sendMsgTemplate(openId, wsm);
        }
    }
}
